#include "core/CodeValidator.h"
#include <QProcess>
#include <QDir>

CodeValidator::CodeValidator() {}
CodeValidator::~CodeValidator() {}

bool CodeValidator::validateProject(const QString &projectPath)
{
    QDir dir(projectPath);
    if (!dir.exists()) {
        return false;
    }
    
    // 检查项目类型并验证
    if (isIOSProject(projectPath)) {
        return validateIOSProject(projectPath);
    } else if (isAndroidProject(projectPath)) {
        return validateAndroidProject(projectPath);
    }
    
    // 默认验证：检查基本文件结构
    return checkProjectStructure(projectPath);
}

bool CodeValidator::checkSyntax(const QString &filePath)
{
    // 简单的语法检查：确保文件可读
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return false;
    }
    file.close();
    return true;
}

bool CodeValidator::checkCompilation(const QString &projectPath)
{
    QProcess process;
    process.setWorkingDirectory(projectPath);
    
    // 检测项目类型并选择合适的编译命令
    if (isIOSProject(projectPath)) {
        // iOS项目使用xcodebuild
        process.start("xcodebuild", QStringList() << "clean" << "build" << "-quiet");
    } else if (isAndroidProject(projectPath)) {
        // Android项目使用gradle
        process.start("./gradlew", QStringList() << "assembleDebug");
    } else {
        // 未知项目类型，返回基本验证
        return checkProjectStructure(projectPath);
    }
    
    // 等待编译完成（最多5分钟）
    if (!process.waitForFinished(300000)) {
        process.terminate();
        return false;
    }
    
    return process.exitCode() == 0;
}

bool CodeValidator::isIOSProject(const QString &projectPath)
{
    QDir dir(projectPath);
    // 检查是否存在.xcodeproj或.xcworkspace文件
    QStringList xcodeFiles = dir.entryList({"*.xcodeproj", "*.xcworkspace"}, QDir::Dirs);
    return !xcodeFiles.isEmpty();
}

bool CodeValidator::isAndroidProject(const QString &projectPath)
{
    QDir dir(projectPath);
    // 检查是否存在build.gradle或settings.gradle文件
    return QFile::exists(projectPath + "/build.gradle") || 
           QFile::exists(projectPath + "/settings.gradle");
}

bool CodeValidator::validateIOSProject(const QString &projectPath)
{
    // 验证iOS项目结构
    QDir dir(projectPath);
    
    // 检查必要的iOS项目文件
    bool hasXcodeProject = !dir.entryList({"*.xcodeproj", "*.xcworkspace"}, QDir::Dirs).isEmpty();
    bool hasInfoPlist = QFile::exists(projectPath + "/Info.plist") || 
                        !dir.entryList({"Info.plist"}, QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot).isEmpty();
    
    if (!hasXcodeProject || !hasInfoPlist) {
        return false;
    }
    
    // 尝试编译验证
    return checkCompilation(projectPath);
}

bool CodeValidator::validateAndroidProject(const QString &projectPath)
{
    // 验证Android项目结构
    bool hasGradleFiles = QFile::exists(projectPath + "/build.gradle") || 
                          QFile::exists(projectPath + "/settings.gradle");
    bool hasManifest = QFile::exists(projectPath + "/app/src/main/AndroidManifest.xml") ||
                       QFile::exists(projectPath + "/src/main/AndroidManifest.xml");
    
    if (!hasGradleFiles || !hasManifest) {
        return false;
    }
    
    // 尝试编译验证
    return checkCompilation(projectPath);
}

bool CodeValidator::checkProjectStructure(const QString &projectPath)
{
    QDir dir(projectPath);
    if (!dir.exists()) {
        return false;
    }
    
    // 检查是否有源代码文件
    QStringList sourcePatterns = {"*.m", "*.mm", "*.swift", "*.java", "*.kt", "*.cpp", "*.h"};
    QStringList sourceFiles = dir.entryList(sourcePatterns, QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
    
    return !sourceFiles.isEmpty();
}